{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After optimizing the parameters for visibility approximation, we need to test how good such approximation is."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-22T10:24:19.603591Z",
     "start_time": "2020-08-22T10:24:19.356643Z"
    }
   },
   "outputs": [],
   "source": [
    "%reset -f\n",
    "%matplotlib notebook\n",
    "%reload_ext autoreload\n",
    "%autoreload 2\n",
    "import os\n",
    "import numpy as np\n",
    "from tqdm import tqdm_notebook\n",
    "\n",
    "import add_path\n",
    "import sampler\n",
    "import visibility as vis\n",
    "import fov_approx_gp as fgp\n",
    "\n",
    "import plotly\n",
    "import plotly.graph_objs as go\n",
    "from plotly.offline import iplot\n",
    "from IPython import display\n",
    "plotly.offline.init_notebook_mode(connected=True)\n",
    "\n",
    "n_test_fs = 300\n",
    "n_test_pws = 20\n",
    "\n",
    "# test the GP approximator just generated\n",
    "uname = 'fov45_fs70_lm1000_k15'\n",
    "param_dir = '/home/zichao/sources/FIF_ws/src/rpg_information_field/act_map/trace'\n",
    "abs_data_dir = os.path.join(param_dir, fgp.FoVApproximatorGP.base_savedir_nm, uname)\n",
    "assert os.path.exists(abs_data_dir), \"{0} does not exist.\".format(abs_data_dir)\n",
    "print('Will load fov approximator from {0}.'.format(abs_data_dir))\n",
    "\n",
    "# alternatively you can also test the provided approximator under params\n",
    "# uname = 'fov45_fs150_lm1000_k15'\n",
    "# param_dir = '/home/zichao/sources/FIF_ws/src/rpg_information_field/act_map/params'\n",
    "# abs_data_dir = os.path.join(param_dir, fgp.FoVApproximatorGP.base_savedir_nm, uname)\n",
    "# assert os.path.exists(abs_data_dir), \"{0} does not exist.\".format(abs_data_dir)\n",
    "# print('Will load fov approximator from {0}.'.format(abs_data_dir))\n",
    "\n",
    "fov_approx = fgp.FoVApproximatorGP(abs_data_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-22T10:24:22.760220Z",
     "start_time": "2020-08-22T10:24:19.608274Z"
    },
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "test_pws = sampler.randomPointsOnSphere(n_test_pws, 3)\n",
    "test_fs = sampler.randomPointsOnSphere(n_test_fs, 1)\n",
    "\n",
    "gt_soft_vis = []\n",
    "pred_soft_vis = []\n",
    "fs_angle = []\n",
    "for pw in tqdm_notebook(test_pws, desc='points'):\n",
    "    Y = fov_approx.calculateY(pw)\n",
    "    c = np.dot(fov_approx.invK, Y)\n",
    "    fs_angle_i = [vis.angleDegBetween(pw, v) for v in test_fs]\n",
    "    fs_angle.extend(fs_angle_i)\n",
    "    \n",
    "    gt_soft_vis_i = [vis.softVisibility(v, pw, fov_approx.hfov_rad, fov_approx.sigmoid_k, fov_approx.fast_sigmoid)\n",
    "                     for v in test_fs]\n",
    "    gt_soft_vis.extend(gt_soft_vis_i)\n",
    "    \n",
    "    pred_soft_vis_i = []\n",
    "    for f in test_fs:\n",
    "        Kreg = fov_approx.calculateKreg(f)\n",
    "        pred_soft_vis_i.append(np.asscalar(np.dot(Kreg, c)))\n",
    "    pred_soft_vis.extend(pred_soft_vis_i)\n",
    "    \n",
    "err = np.array(gt_soft_vis) - np.array(pred_soft_vis)\n",
    "print(\"The error: {0} - {1}\".format(np.min(err), np.max(err)))\n",
    "\n",
    "trace_gt = go.Scatter(x=fs_angle, y=gt_soft_vis, mode = 'markers', name='Soft Visibility Groundtruth')\n",
    "trace_predict = go.Scatter(x=fs_angle, y=pred_soft_vis, mode = 'markers', name='Predicted',\n",
    "                           marker=dict(color='rgba(0, 120, 0, 0.3)'))\n",
    "\n",
    "fig = plotly.tools.make_subplots(rows=1, cols=1)\n",
    "fig.append_trace(trace_gt, 1, 1)\n",
    "fig.append_trace(trace_predict, 1, 1)\n",
    "iplot(fig)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.17"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "position": {
    "height": "494px",
    "left": "1546px",
    "right": "20px",
    "top": "120px",
    "width": "370px"
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
